#ifndef MODBUSSTARTINSTRUCTIONLOGIC_H
#define MODBUSSTARTINSTRUCTIONLOGIC_H

#include <QObject>
#include "instructionEnum.h"
#include "GeneralDefine.h"
#include "modbus.h"
#include <QMap>

#define OK 1
#define ERROR -1
#define D_MODBUS_CONFIG_FILE_PATH "/agvwork/AgvFundation/fundation/data/globalConfig/programLogic/modbusLogic.ini"
class ProgramSet;
class ProgramEngine;
class RegData;
class ModbusStartInstruction;



class ModbusStartInstructionLogic : public QObject
{
    Q_OBJECT
public:
    explicit ModbusStartInstructionLogic(int robotIdIn,
                                         ProgramSet *programSetIn,
                                         ProgramEngine *programEngineIn,
                                         RegData *regDataIn);

    /**
     * @brief startModbusStart
     * @param programNumIn
     * @param instructionIn
     * @param lineIndex
     * @return
     */
    E_PROGRAM_LOOP_RESULT startModbusStart(int programNumIn,const ModbusStartInstruction &instructionIn, int lineIndex);


private:

    // 初始化Modbus设备
    int initialModbusDevice();
    // 开始建立Rs485主站模式
    int start_Rs485Master();
    // 开始建立Rs485从站模式
    int start_Rs485Slave();
    // 开始建立以太网主站模式client
    int start_InternetMaster();
    // 开始建立以太网从站模式Server
    int start_InternetSlave(int portIn);

    int readRs485MasterConfig(const QString &filePath);
    int readRs485SlaveConfig(const QString &filePath);
    int readInternetMasterConfig(const QString &filePath);
    int readInternetSlaveConfig(const QString &filePath);

private:

    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0,
                 QString message= "");

private:

    ProgramEngine *programEngine;   // 程序引擎对象
    ProgramSet *programSet;         // 程序集合对象
    RegData *regData;               // 寄存器对象
    int robotId;                    // 机器人编号

    bool isInitialModbusDevice;     // 初始化设备


};

#endif // MODBUSSTARTINSTRUCTIONLOGIC_H
